package com.vhall.component.service.room.event.handler;

import cn.hutool.http.*;
import com.alibaba.fastjson.JSONObject;
import com.vhall.component.plugin.common.utils.algorithm.PaasCallbackMd5SignValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author jingwen.li
 */
@Slf4j
@Component
public class RedirectUtil {
    @Value("${paas.apps.lite.appSecret}")
    private String appSecret;

    public void redirect(String url, Map<String, Object> callBack) {
        if (!url.startsWith("http")) {
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.putAll(callBack);
            String sign = PaasCallbackMd5SignValidator.sign(jsonObject, appSecret).toLowerCase();
            jsonObject.put("signature", sign);
            log.info("将回调数据转发到老系统,url={},callback:{}", url, jsonObject);
            HttpRequest httpRequest = HttpUtil.createPost(url).contentType(ContentType.JSON.toString()).body(jsonObject.toString());
            HttpResponse httpResponse = httpRequest.execute();
            log.info("将回调数据转发到老系统,response={}", httpResponse.body());
        } catch (Exception e) {
            log.info("将回调数据转发到老系统,异常={}", e);
            e.printStackTrace();
        }

    }
}
